package com.ys.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ys.exception.YsLjException;
import com.ys.exception.YsLjExceptionEnum;
import com.ys.model.dao.ChangeMapper;
import com.ys.model.dao.ComponentMapper;
import com.ys.model.dao.FactoryMapper;
import com.ys.model.dao.UserMapper;
import com.ys.model.pojo.*;
import com.ys.model.request.RepairSearchReq;
import com.ys.model.vo.ChangeDetailVo;
import com.ys.model.vo.SearchVo;
import com.ys.service.ChangeService;
import com.ys.util.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ChangeServiceImpl implements ChangeService {

    @Autowired
    ChangeMapper changeMapper;

    @Autowired
    ComponentMapper componentMapper;

    @Autowired
    FactoryMapper factoryMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 更换更新
     * @param changeVo
     */
    @Override
    public void updateChange(ChangeVo changeVo)
    {


        if (changeVo.getNewCcode() != null)
        {
            Component componentOld = componentMapper.selectByCode(changeVo.getNewCcode());
            if (componentOld != null)
            {
                throw new YsLjException(YsLjExceptionEnum.LJ_EXISTED);
            }
        }

        User userOld = userMapper.selectByName(changeVo.getChangePeople());

        if (userOld != null) {

            int id = userOld.getuId();

            Component oldComonet = componentMapper.selectByPrimaryKey(changeVo.getCid());

            if (oldComonet == null) {
                throw  new YsLjException(YsLjExceptionEnum.UPDATE_FAILED);
            }

           /* Component newComonet = new Component();
            newComonet.setcId(changevo.getCid());
            newComonet.setcName(changevo.getNewCname());
            newComonet.setfName(changevo.getNewCfactory());
            newComonet.setcCode(changevo.getNewCcode());
            newComonet.setcType(changevo.getNewCtype());
            newComonet.setStarttime(changevo.getStartTime());*/

            Factory factory = factoryMapper.selectByFactoryName(changeVo.getNewCfactory());

            if (factory == null) {
                factoryMapper.insertC_FName(changeVo.getNewCfactory());

                Factory f  = factoryMapper.selectByFactoryName(changeVo.getNewCfactory());
                Component newComonet = new Component();
                newComonet.setcId(changeVo.getCid());
                newComonet.setcName(changeVo.getNewCname());
                newComonet.setfName(changeVo.getNewCfactory());
                newComonet.setcCode(changeVo.getNewCcode());
                newComonet.setcType(changeVo.getNewCtype());
                newComonet.setStarttime(changeVo.getStartTime());
                newComonet.setfId(f.getfId());
                newComonet.setcLocation(changeVo.getcLocation());
                componentMapper.updateBySelective(newComonet);

                Change change = new Change();
                change.setcPeopleId(id);
                change.setcTime(changeVo.getStartTime());
                change.setCcId(changeVo.getCid());
                change.setOcCode(oldComonet.getcCode());
                change.setOcFactory(oldComonet.getfName());
                change.setOcName(oldComonet.getcName());
                change.setOcType(oldComonet.getcType());
                change.setcLocation(changeVo.getcLocation());

                StringBuilder changContent = new StringBuilder();
                if (!"".equals(changeVo.getNewCname()) && null != changeVo.getNewCname()) {
                    changContent.append("零件名字由").append(oldComonet.getcName()).append("修改为").append(changeVo.getNewCname());
                }
                if (!"".equals(changeVo.getNewCfactory()) && null != changeVo.getNewCfactory()) {
                    changContent.append("零件工厂由").append(oldComonet.getcName()).append("修改为").append(changeVo.getNewCfactory());
                }
                if (!"".equals(changeVo.getNewCcode()) && null != changeVo.getNewCcode()) {
                    changContent.append("零件编码由").append(oldComonet.getcCode()).append("修改为").append(changeVo.getNewCcode());
                }
                if (!"".equals(changeVo.getNewCtype()) && null != changeVo.getNewCtype()) {
                    changContent.append("零件类型由").append(oldComonet.getcType()).append("修改为").append(changeVo.getNewCtype());
                }

                change.setcContent(changContent.toString());

                changeMapper.updateByPrimaryKeySelective(change);

                //changeMapper.insert(change);

                //throw new YsLjException(YsLjExceptionEnum.FOCTORY_UPDATE_FAILED);
            }else {
                Factory oldFactory = factoryMapper.selectByPrimaryKey(oldComonet.getfId());
                Component newComonet = new Component();
                newComonet.setcId(changeVo.getCid());
                newComonet.setcName(changeVo.getNewCname());
                newComonet.setfName(changeVo.getNewCfactory());
                newComonet.setcCode(changeVo.getNewCcode());
                newComonet.setcType(changeVo.getNewCtype());
                newComonet.setStarttime(changeVo.getStartTime());
                newComonet.setfId(factory.getfId());
                newComonet.setcLocation(changeVo.getcLocation());
                componentMapper.updateBySelective(newComonet);

                Change change = new Change();
                change.setcPeopleId(id);
                change.setcTime(changeVo.getStartTime());
                change.setCcId(changeVo.getCid());
                change.setOcCode(oldComonet.getcCode());
                change.setOcFactory(oldFactory.getfName());
                change.setOcName(oldComonet.getcName());
                change.setOcType(oldComonet.getcType());
                change.setcLocation(changeVo.getcLocation());

                StringBuilder changContent = new StringBuilder();
                if (!"".equals(changeVo.getNewCname()) && null != changeVo.getNewCname()) {
                    changContent.append("零件名字由").append(oldComonet.getcName()).append("修改为").append(changeVo.getNewCname());
                }
                if (!"".equals(changeVo.getNewCfactory()) && null != changeVo.getNewCfactory()) {
                    changContent.append("零件工厂由").append(oldComonet.getcName()).append("修改为").append(changeVo.getNewCfactory());
                }
                if (!"".equals(changeVo.getNewCcode()) && null != changeVo.getNewCcode()) {
                    changContent.append("零件编码由").append(oldComonet.getcCode()).append("修改为").append(changeVo.getNewCcode());
                }
                if (!"".equals(changeVo.getNewCtype()) && null != changeVo.getNewCtype()) {
                    changContent.append("零件类型由").append(oldComonet.getcType()).append("修改为").append(changeVo.getNewCtype());
                }

                change.setcContent(changContent.toString());

                changeMapper.updateByPrimaryKeySelective(change);
                //changeMapper.updateBySelective(change);
                //changeMapper.insert(change);
            }

        } else {
            throw new YsLjException(YsLjExceptionEnum.UPDATE_FAILED);
        }

    }

    /*I***************************************/


    @Override
    public List<SearchVo> changeSearchList() {
        List<SearchVo> searchVos = new ArrayList<>();
        return changeMapper.changeSearchList();
    }


    @Override
    public void deleteById(Integer id) {
        changeMapper.deleteById(id);
    }

    @Override
    public List<ChangeDetailVo> search(RepairSearchReq repairSearchReq) {
        List<ChangeDetailVo> searchList = changeMapper.search(repairSearchReq);
        return searchList;
    }

    @Override
    public int getChangeId(Date date) {
        return changeMapper.getChangeId(date);
    }

    @Override
    public void changePictures(String pName, Integer changeId) {
        changeMapper.changePictures(pName,changeId);
    }


}
